package JAVA300.第十章IO;

import java.io.File;

/**
 * 测试文件夹相关操作
 */

public class DirDemo01 {

    public static long length=0;
    public static void main(String[] args) {
        //文件夹操作
        File file3 = new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src/JAVA301/testDirCreates");

        System.out.println(file3.mkdir());
        System.out.println(file3.mkdirs());

        file3.delete();

        //下级
        File file = new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src");
        System.out.println("-----------------");
        String[] subName = file.list();//名称
        for (String name:subName){
            System.out.println(name);
        }

        File[] sunFile = file.listFiles();//对象
        for (File file1:sunFile){
            System.out.println(file1.getName());
        }

        //利用递归函数实现所有文件名输出
        System.out.println("-----------------");
        File fileSearch = new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src/JAVA300");
        PrintName(fileSearch,0);
        DirCount(fileSearch);
        System.out.println(length);
    }


    //递归遍历文件名函数
    public static void PrintName(File file,int deep){

        for (int i=0;i<deep;i++){
            System.out.print("-");
        }
        System.out.println(file.getName());//先输出当前名称 再往下遍历
        if (file == null || !file.exists()){
            return;
        }else if (file.isDirectory()){
            for (File listname:file.listFiles()){

                PrintName(listname,deep+1);
                //System.out.println(listname.getName());
            }
        }
    }

    public static void DirCount(File file){

        if (file == null || !file.exists()){
            return;
        }else{

            if (file.isDirectory()){
                for (File temp:file.listFiles()){
                    DirCount(temp);
                }
            }else {
                length = length + file.length();

            }
        }
    }
}
